package com.example.warehousewms.service.impl;
import java.time.LocalDate;
import java.time.LocalDateTime;

import com.example.warehousewms.bean.Inventory;
import com.example.warehousewms.bean.StockInOrder;
import com.example.warehousewms.bean.StockInOrderItem;
import com.example.warehousewms.bean.vo.StockInOrderVo;
import com.example.warehousewms.mapper.InventoryMapper;
import com.example.warehousewms.mapper.StockInOrderItemMapper;
import com.example.warehousewms.mapper.StockInOrderMapper;
import com.example.warehousewms.service.StockInOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 入库单服务实现类
 */
@Service
public class StockInOrderServiceImpl implements StockInOrderService {

    @Autowired
    private StockInOrderMapper stockInOrderMapper;

    @Autowired
    private StockInOrderItemMapper stockInOrderItemMapper;

    @Autowired
    private InventoryMapper inventoryMapper;

    @Transactional
    @Override
    public void addStockInOrder(StockInOrderVo stockInOrderVo) {
        // 创建入库单的主表
        StockInOrder stockInOrder = new StockInOrder();

        //生成入库单号
        stockInOrder.setOrderNo("RKD" + System.currentTimeMillis());
        stockInOrder.setOrderType(stockInOrderVo.getOrderType());
        stockInOrder.setSupplierId(stockInOrderVo.getSupplierId());
        stockInOrder.setWarehouseId(stockInOrderVo.getWarehouseId());
        stockInOrder.setRelatedOrderNo("");
        stockInOrder.setTotalQuantity(stockInOrderVo.getTotalQuantity());
        stockInOrder.setActualTotalQuantity(0);
        stockInOrder.setOrderStatus(stockInOrderVo.getOrderStatus());
        stockInOrder.setReceiptTime(LocalDateTime.now());
        stockInOrder.setRemark("");
        stockInOrder.setCreateTime(LocalDateTime.now());
        stockInOrder.setUpdateTime(LocalDateTime.now());

        stockInOrderMapper.insertStockInOrder(stockInOrder);

        Long stockInOrderId = stockInOrder.getId();

        // 创建入库单的子表

        List<StockInOrderItem> list = new ArrayList<>();

        for (StockInOrderItem s : stockInOrderVo.getStockInOrderItemList()){

            StockInOrderItem stockInOrderItem = new StockInOrderItem();

            stockInOrderItem.setStockInOrderId(stockInOrderId);
            stockInOrderItem.setProductId(s.getProductId());
            stockInOrderItem.setPlannedQuantity(s.getPlannedQuantity());
            stockInOrderItem.setProductionDate(s.getProductionDate());
            stockInOrderItem.setRemark("");
            stockInOrderItem.setCreateTime(LocalDateTime.now());
            stockInOrderItem.setUpdateTime(LocalDateTime.now());

            list.add(stockInOrderItem);

        }
        stockInOrderItemMapper.insertStockInOrderItems(list);
    }

    @Transactional
    @Override
    public void updateStockInOrder(StockInOrderVo stockInOrderVo, Long id) {
        StockInOrder stockInOrder = new StockInOrder();
        stockInOrder.setId(id);
        stockInOrder.setOrderType(stockInOrderVo.getOrderType());
        stockInOrder.setSupplierId(stockInOrderVo.getSupplierId());
        stockInOrder.setWarehouseId(stockInOrderVo.getWarehouseId());
        stockInOrder.setRelatedOrderNo(stockInOrderVo.getRelatedOrderNo());
        stockInOrder.setTotalQuantity(stockInOrderVo.getTotalQuantity());
        stockInOrder.setActualTotalQuantity(stockInOrderVo.getActualTotalQuantity());
        stockInOrder.setOrderStatus(stockInOrderVo.getOrderStatus());
        stockInOrder.setUpdateTime(LocalDateTime.now());

        stockInOrderMapper.updateStockInOrder(stockInOrder);

        if(stockInOrderVo.getStockInOrderItemList() != null && stockInOrderVo.getStockInOrderItemList().size() > 0){
            List<StockInOrderItem> list = new ArrayList<>();

            for (StockInOrderItem s : stockInOrderVo.getStockInOrderItemList()){

                StockInOrderItem stockInOrderItem = new StockInOrderItem();

                stockInOrderItem.setId(s.getId());
                stockInOrderItem.setStockInOrderId(s.getStockInOrderId());
                stockInOrderItem.setProductId(s.getProductId());
                stockInOrderItem.setPlannedQuantity(s.getPlannedQuantity());
                stockInOrderItem.setActualQuantity(s.getActualQuantity());
                stockInOrderItem.setLocationId(s.getLocationId());
                stockInOrderItem.setProductionDate(s.getProductionDate());
                stockInOrderItem.setRemark("");
                stockInOrderItem.setUpdateTime(LocalDateTime.now());

                list.add(stockInOrderItem);
            }
            stockInOrderItemMapper.updateStockInOrderItems(list);
        }

    }

    @Override
    public StockInOrder getStockInOrderById(Long id) {
        return stockInOrderMapper.getStockInOrderById(id);
    }

    @Override
    public List<StockInOrder> getAllStockInOrder() {
        return null;
    }

    @Transactional
    @Override
    public void finishStockInOrder(Long id) {
        StockInOrder stockInOrder = stockInOrderMapper.getStockInOrderById(id);
        // 判断状态
        if (stockInOrder == null || !"APPROVED".equals(stockInOrder.getOrderStatus())){
            throw new RuntimeException("该入库单状态无法完成");
        }

        List<StockInOrderItem> items = stockInOrderItemMapper.getStockItemByStockOrderId(stockInOrder.getId());
        for (StockInOrderItem s : items){
            Inventory inventory = inventoryMapper.getInventoryByProductIdAndLocationId(
                    s.getProductId(),
                    s.getLocationId());

            if (inventory == null){
                // 创建一个库存
                inventory = new Inventory();
                inventory.setProductId(s.getProductId());
                inventory.setLocationId(s.getLocationId());
                inventory.setQuantity(s.getActualQuantity());
                inventory.setLockedQuantity(10);
                inventory.setInventoryStatus("NORMAL");
                inventory.setLastStockInTime(LocalDateTime.now());

                inventoryMapper.insertInventory(inventory);
            }else{
                // 更新库存
                inventory.setQuantity(inventory.getQuantity() + s.getActualQuantity());
                inventory.setLastStockInTime(LocalDateTime.now());
                inventoryMapper.updateInventory(inventory);
            }

        }
        // 修改入库单状态
        stockInOrder.setOrderStatus("COMPLETED");
        stockInOrderMapper.updateStockInOrder(stockInOrder);
    }

    @Override
    public void cancelStockInOrder() {

    }
}
